
/*
http://www.kkh86.com/it/chrome-extension-doc/extensions/api_index.html
http://chrome.noonme.com/extensions/bookmarks.html
 */

chrome.bookmarks.onChanged.addListener(function (changeInfo) {
    console.log('书签发生了变化：', changeInfo);
});

//chromeBookmarksGetTree
function chromeBookmarksGetTree() {
    return new Promise(function (resolve, reject) { chrome.bookmarks.getTree(function (bookmarkTreeNodes) { resolve(bookmarkTreeNodes); });});
}
// 获取书签树
function getTreeChromeBookmarks(fun) {
    chrome.bookmarks.getTree(function (bookmarkTreeNodes) { fun(bookmarkTreeNodes) });
}

/** 
 * 创建数据
 * 
 * 
 * 
 * 
*/
function createChromeBookmarks(bookmark, callback) {
    chrome.bookmarks.create(bookmark, callback())
}

/**
 * 
 * @param {*} bookmarks 
 * @param {*} parentId 
 */
function createChromeBookmarksTree(bookmarks, parentId) {
    //console.log(bookmarks);
    for (var i = 0; i < bookmarks.length; i++) {
        let parentIds = parentId == 0 ? bookmarks[i].parentId : parentId;
        let obj = bookmarks[i];
        chrome.bookmarks.create({
            parentId: parentIds,
            title: bookmarks[i].title,
            url: bookmarks[i].url,
            index: i,
            //id: bookmarks[i].id,
            //children: bookmarks[i].children
        }, function (result) {
            if (obj.hasOwnProperty("children")) {
                if (obj.children.length > 0) {
                    createChromeBookmarksTree(obj.children, result.id)
                }
            }
        })
    }
}

/**
 * 删除书签
 * @param {*} bookmarkTreeNodes 
 * @param {*} callback 
 */
function removeTreeChromebookmarks(bookmarkTreeNodes, callback) {
    chrome.bookmarks.removeTree(bookmarkTreeNodes, function () { })
}

//存储 get
function getChromeStorage(key, callback) {
    chrome.storage.sync.get(key, function (result) {
        callback(result[key]);
    });
}

function getChromeStorageSync(key) {
    return new Promise(function (resolve, reject) {
        chrome.storage.sync.get(key, function (result) {
            resolve(result[key]);
        });
    });
}

//存储 set
function setChromeStorageSync(key, value) {
   return new Promise(function (resolve, reject) {
        chrome.storage.sync.set({
            [key]: value,
        }, function () {
            resolve("设置配置完成");
        });
    });
}

function setChromeStorage(key, value, callback) {
    chrome.storage.sync.set({
        [key]: value,
    }, function () {
        callback();
    });
}

//通知
function createChromeNotifications(id, options, creationCallback) {
    chrome.notifications.create(id, options, function () { });
}

function createChromeNotification2(id, options, creationCallback) {
    chrome.notifications.create(id, options, function () { });
}

//事件
chrome.bookmarks.onCreated.addListener(function (id, bookmark) {
    var opt = {
        type: "basic",
        title: "同步成功",
        message: "同步成功",
        iconUrl: "img/icons/icon64.png"
    };
    createChromeNotifications("测试", opt, function () { });
});

chrome.bookmarks.onChanged.addListener(function(id, changeInfo) {
    console.log('Bookmark changed:', id, changeInfo);
    alert('Bookmark changed:')
});